package com.block.datapush.service.apipush;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.block.common.model.param.PushApiKeyParam;
import com.block.common.service.IMemberCommonService;
import com.block.common.service.LogFactory;
import com.block.datapush.enums.PushStatusEnum;
import com.block.datapush.model.PushQueryParam;
import com.block.datapush.model.dto.NoticeParamDTO;
import com.block.datapush.model.result.ProtocolParamResult;
import com.block.datapush.model.result.PushApiResult;
import com.block.datapush.service.IPushApiService;
import com.block.datapush.service.apipush.utils.NingBoUtil;
import com.block.db.entity.MemberSourceInfo;
import com.block.db.entity.MerchantBaseInfo;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 宁波700+
 */
@Service
public class NingBoApiServicea implements IPushApiService {

    private static final Logger log = LogFactory.getPushLog();

    @Autowired
    IMemberCommonService memberCommonService;

    @Override
    public PushApiResult checkUser(String orderNo, PushApiKeyParam pushApiKeyParam, MemberSourceInfo memberSourceInfo, JSONObject extJson) throws Exception {

        PushApiResult pushApiResult = new PushApiResult();
        pushApiResult.setPushSuccess(false);


        String queryUrl = pushApiKeyParam.getCheckUrl();

        String mobileMd5 = memberSourceInfo.getMobileMd5();

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("md5mobile", mobileMd5);
        paramMap.put("brand", pushApiKeyParam.getApiCode());


        log.info("【宁波700+】撞库开始【queryUrl】{}【paramMap】{}", queryUrl, JSON.toJSONString(paramMap));
        String result = HttpUtil.createPost(queryUrl)
                .form(paramMap)
                .execute().body();
        log.info("【宁波700+】撞库结束【result】{}", result);


        pushApiResult.setCode(result);
        pushApiResult.setMessage(result);
        pushApiResult.setParamInfo(JSON.toJSONString(paramMap));
        pushApiResult.setResultInfo(result);

        if (StrUtil.equals(String.valueOf(result), "1")) {
            pushApiResult.setPushSuccess(true);
            return pushApiResult;
        }

        return pushApiResult;
    }

    @Override
    public PushApiResult queryApprove(String orderNo, PushApiKeyParam pushApiKeyParam, MemberSourceInfo memberSourceInfo, JSONObject extJson) throws Exception {
        PushApiResult pushApiResult = new PushApiResult();
        pushApiResult.setPushSuccess(false);
        pushApiResult.setOrderNo(orderNo);
        return pushApiResult;
    }

    @Override
    public PushApiResult registerUser(String orderNo, String outOrderNo, PushApiKeyParam pushApiKeyParam, MemberSourceInfo memberSourceInfo, JSONObject extJson) throws Exception {
        PushApiResult pushApiResult = new PushApiResult();
        pushApiResult.setPushSuccess(false);

        String requestUrl = pushApiKeyParam.getRegisterUrl();

        String city = memberSourceInfo.getCity();
        if (StrUtil.endWith(city, "市")) {
            city = memberSourceInfo.getCity().substring(0, memberSourceInfo.getCity().length() - 1);
        }
        String sex = memberSourceInfo.getSex() == 1 ? "男" : "女";
        Long time = System.currentTimeMillis() / 1000;

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("name", memberSourceInfo.getRealname());
        paramMap.put("mobile", memberSourceInfo.getMobile());
        paramMap.put("city", city);
        paramMap.put("car", NingBoUtil.getCar(memberSourceInfo.getCar()));
        paramMap.put("age", memberSourceInfo.getAge());
        paramMap.put("job", "有");
        paramMap.put("house", NingBoUtil.getHouse(memberSourceInfo.getHouse()));
        paramMap.put("baodan_is", NingBoUtil.getBaoDan(memberSourceInfo.getBaoxian()));
        paramMap.put("sex", sex);
        paramMap.put("money", NingBoUtil.getloan(memberSourceInfo.getLoanAmount()));
        paramMap.put("source", pushApiKeyParam.getApiKey());
        paramMap.put("source_remark", "有打卡工资");
        paramMap.put("shebao", NingBoUtil.getSheBao(memberSourceInfo.getShebao()));
        paramMap.put("gongjijin", NingBoUtil.getGjj(memberSourceInfo.getGjj()));
        paramMap.put("credit_card", "无");
        paramMap.put("company", NingBoUtil.getGongZuo(memberSourceInfo.getGongzuo()));
        paramMap.put("zhima", NingBoUtil.getzhima(memberSourceInfo.getZhima()));
        paramMap.put("isbankpay", "银行代发");
        paramMap.put("time", time);


        log.info("【宁波700+】导单开始【requestUrl】{}【bizMap】{}", requestUrl, JSON.toJSONString(paramMap));
        String result = HttpUtil.createPost(requestUrl)
                .form(paramMap)
                .execute().body();
        log.info("【宁波700+】导单结束【result】{}", result);


        pushApiResult.setCode(result);
        pushApiResult.setMessage(result);
        pushApiResult.setParamInfo(JSON.toJSONString(paramMap));
        pushApiResult.setResultInfo(result);

        if (Integer.valueOf(result) > 100) {
            pushApiResult.setPushSuccess(true);
            pushApiResult.setSettleFlag(true);
            pushApiResult.setStatus(PushStatusEnum.SUCCESS.getValue());
            return pushApiResult;
        }
        pushApiResult.setPushSuccess(false);
        pushApiResult.setSettleFlag(false);
        pushApiResult.setStatus(PushStatusEnum.FAILED.getValue());
        return pushApiResult;
    }

    @Override
    public PushApiResult queryResult(PushQueryParam pushQueryParam, PushApiKeyParam pushApiKeyParam, String extJson) throws Exception {
        PushApiResult pushApiResult = new PushApiResult();
        pushApiResult.setPushSuccess(false);
        return pushApiResult;
    }

    @Override
    public NoticeParamDTO getNoticeResultParam(String infoId, Map<String, String[]> parameterMap, String paramContent) {
        NoticeParamDTO noticeParamDTO = new NoticeParamDTO();
        return noticeParamDTO;
    }

    @Override
    public JSONObject getNoticeResultReturn(Map<String, String[]> parameterMap, String paramContent, JSONObject extObject) {
        JSONObject jsonObject = new JSONObject();
        return jsonObject;
    }


    @Override
    public ProtocolParamResult getProtocolResult(MerchantBaseInfo merchantBaseInfo, MemberSourceInfo memberSourceInfo) {
        return null;
    }
    

}
